Emit chat state changed event when chat state changes (e.g. one contact starts typing) #3374
+54
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Details
The PR adds the functionality requested on #82 and on related issues: #1137, #2636, #3372.
This feature introduces the ability to detect chat states such as 'available', 'unavailable', 'typing' and 'recording', addressing a longstanding request from the community
Description
3 main changes were made:
CHAT_STATE_CHANGED
event on the Events object fromsrc/utiol/Contants.js
src/Client.js:249
injectPresenceModelInstance
: this function wraps the underlying modelClass from the Store.Presence object. the modelClass is used to generate all the models that represents a presence for a given chat.This is done to "inject" the get and set properties on every single instance generate my the model class to be able to call the emit function every time the model chat state property is update
execPresenceModelInjection
: this function is called when the Store.Presence object becomes available (it takes a couple of milliseconds for it to load) and calls theinjectPresenceModelInstance
to inject every model already created.The function also makes sure to maintain the prototype chain of every injected object to avoid breaking underlying functionality
Motivation and Context
The main objective is to have one more way to improve user experience of apps built using whatapp-web.js. By providing a way to handle events related to chat state changes developers will be able to create interaction flows with a finer level of control.
An example use case would be: prevent an automatic response from being sent if the target user is typing, so it doesn't interrupt the user.
How Has This Been Tested
I tested manually using the
shell
command from thepackage.json
file.Actions:
npm run shell
client.on('chat_state_changed', console.log)
{ chatId: '*********@c.us', chatState: 'typing' }
{ chatId: '*********@c.us', chatState: 'unavailable' }
Environment
Types of changes
Checklist
I have updated the documentation accordingly (index.d.ts).